/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package wekimini.learning;

import java.awt.CardLayout;
import java.util.logging.Logger;
import wekimini.util.Util;

/**
 *
 * @author rebecca
 */
public class LinearRegressionEditorPanel extends LearningModelBuilderEditorPanel {

    private static final Logger logger = Logger.getLogger(LinearRegressionEditorPanel.class.getName());

    /**
     * Creates new form LinearRegressionEditorPanel
     */
    public LinearRegressionEditorPanel() {
        initComponents();
    }

    public LinearRegressionEditorPanel(LinearRegressionModelBuilder mb) {
        initComponents();
        if (mb.getFeatureSelectionType() == LinearRegressionModelBuilder.FeatureSelectionType.NONE) {
            comboSelectionType.setSelectedIndex(0);
        } else if (mb.getFeatureSelectionType() == LinearRegressionModelBuilder.FeatureSelectionType.M5) {
            comboSelectionType.setSelectedIndex(1);
        } else {
            //Greedy
            comboSelectionType.setSelectedIndex(2);
        }

        if (mb.isRemoveColinear()) {
            comboRemoveColinear.setSelectedIndex(1);
        } else {
            comboRemoveColinear.setSelectedIndex(0);
        }

        int exp = mb.getExponent();
        if (exp == 1) {
            textExponent.setText(Integer.toString(2));
            comboIsLinear.setSelectedIndex(0);
        } else {
            textExponent.setText(Integer.toString(exp));
            comboIsLinear.setSelectedIndex(1);
        }
        updatePanel();

    }

    private void updatePanel() {
        if (comboIsLinear.getSelectedIndex() == 0) {
            ((CardLayout) panelExponent.getLayout()).show(panelExponent, "cardNone");
        } else {
            ((CardLayout) panelExponent.getLayout()).show(panelExponent, "cardPolynomial");
        }
    }

    @Override
    public boolean validateForm() {
        if (comboIsLinear.getSelectedIndex() == 1) {
            try {
                int i = Integer.parseInt(textExponent.getText());
                if (i <= 1) {
                    Util.showPrettyErrorPane(this, "Exponent must be an integer greater than 1");
                    return false;
                }
            } catch (NumberFormatException ex) {
                Util.showPrettyErrorPane(this, "Exponent must be an integer greater than 1");
                return false;
            }
        }
        return true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        panelExponent = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        textExponent = new javax.swing.JTextField();
        comboIsLinear = new javax.swing.JComboBox();
        comboSelectionType = new javax.swing.JComboBox();
        comboRemoveColinear = new javax.swing.JComboBox();

        setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        panelExponent.setLayout(new java.awt.CardLayout());

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 432, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 61, Short.MAX_VALUE)
        );

        panelExponent.add(jPanel4, "cardNone");

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jLabel3.setText("Polynomial exponent (positive int):");

        textExponent.setText("1");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textExponent, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(textExponent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        panelExponent.add(jPanel2, "cardPolynomial");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelExponent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelExponent, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
        );

        comboIsLinear.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Linear in inputs (default)", "Polynomial regression" }));
        comboIsLinear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboIsLinearActionPerformed(evt);
            }
        });

        comboSelectionType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "No feature selection (default)", "M5 feature selection", "Greedy feature selection" }));
        comboSelectionType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboSelectionTypeActionPerformed(evt);
            }
        });

        comboRemoveColinear.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Do not remove colinear inputs (recommended for polynomial)", "Remove colinear inputs" }));
        comboRemoveColinear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboRemoveColinearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboIsLinear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(comboSelectionType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(comboRemoveColinear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(comboIsLinear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboSelectionType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboRemoveColinear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void comboSelectionTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboSelectionTypeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_comboSelectionTypeActionPerformed

    private void comboRemoveColinearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboRemoveColinearActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_comboRemoveColinearActionPerformed

    private void comboIsLinearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboIsLinearActionPerformed
        updatePanel();
    }//GEN-LAST:event_comboIsLinearActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboIsLinear;
    private javax.swing.JComboBox comboRemoveColinear;
    private javax.swing.JComboBox comboSelectionType;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel panelExponent;
    private javax.swing.JTextField textExponent;
    // End of variables declaration//GEN-END:variables

    //Assumes form is valid
    @Override
    public LinearRegressionModelBuilder buildFromPanel() {
        LinearRegressionModelBuilder.FeatureSelectionType fs;
        if (comboSelectionType.getSelectedIndex() == 0) {
            fs = LinearRegressionModelBuilder.FeatureSelectionType.NONE;
        } else if (comboSelectionType.getSelectedIndex() == 1) {
            fs = LinearRegressionModelBuilder.FeatureSelectionType.M5;
        } else {
            fs = LinearRegressionModelBuilder.FeatureSelectionType.GREEDY;
        }
        LinearRegressionModelBuilder mb = new LinearRegressionModelBuilder();
        mb.setFeatureSelectionType(fs);
        if (comboIsLinear.getSelectedIndex() == 0) {
            mb.setExponent(1);
        } else {
            mb.setExponent(Integer.parseInt(textExponent.getText()));
        }
        mb.setRemoveColinear(comboRemoveColinear.getSelectedIndex() == 1);

        return mb;
    }
}
